<?php

class postDetails {

    var $article;
    var $title = null;
    var $date = null;
    var $author = null;
    var $category = null;
    var $summary = null;
    var $body = null;
    var $links = null;
    var $next = null;
    var $prev = null;

    function set_page($new_page) {
        $this->pageid = $new_page;
        return $this->pageid;
    }

    function set_id($new_id) {
        $this->id = $new_id;
        return $this->id;
    }

    function check_id() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT title FROM ".$prefix."posts WHERE id ='".$this->id."'")) {
            $numrows = $result->num_rows;
            if($numrows > 0) {
                $this->checkId = true;
            }
            else {
                $this->checkId = false;
            }
            return $this->checkId;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function check_draft() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT draft FROM ".$prefix."posts WHERE id = ".$this->id."")) {
            $row = $result->fetch_object();
            if($row->draft == "0") {
                return true;
            }
            else {
                return false;
            }
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_thumb() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT thumb FROM ".$prefix."posts WHERE id = ".$this->id."")) {
            $row = $result->fetch_object();
            $this->thumb = $row->thumb;
            return $this->thumb;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_title() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT title FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->title = $row->title;
            return $this->title;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_date() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT time FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->date = date('l F jS Y m:s',strtotime($row->time));
            return $this->date;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_author() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT author FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            if($row->author == $_SESSION['username']) {
                $this->author = "You";
            }
            else {
                $this->author = $row->author;
            }
            return $this->author;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_realAuthor() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT author FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->realAuthor = $row->author;
            return $this->realAuthor;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_authorId() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT author FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $result2 = $connect->query("SELECT id FROM ".$prefix."users WHERE username = '".$row->author."'");
            $userRow = $result2->fetch_object();
            $this->authorId = $userRow->id;
            return $this->authorId;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_category() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT category FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->category = $row->category;
            return $this->category;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_categoryList() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT category FROM ".$prefix."posts GROUP BY category ORDER BY category")) {
            while($row = $result->fetch_object()) {
                if($row->category != $this->category) {
                    $this->catList[] = array(
                        "category" => $row->category
                    );
                }
            }
            return $this->catList;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_tags() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT tags FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            if(!empty($row->tags)) {
                $this->tags = $row->tags;
                $urls = explode(', ', $this->tags);
                foreach($urls as $key => $value) {
                    $tag[$key] = '<li><a href="page/201/archive/tag/'.strtolower($value).'">'.ucfirst($value).'</a></li>';
                }
                $this->tags = implode('<li> | </li>', $tag);
                return $this->tags;
            }
            else {
                return "";
            }
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_tagList() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT tags FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->tagList = $row->tags;
            return $this->tagList;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_similar() {
        $tags = explode(', ',$this->tags);
        foreach($tags as $tag) {
            $queryPiece = "(category LIKE '%$this->category%' OR tags LIKE '%$tag%') AND";
        }
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("
            SELECT
               SUBSTRING(title, 1, 30 )AS shortTitle,
               title,
               id,
               category,
               time
            FROM
               posts
            WHERE
        $queryPiece
               (draft = '0')
            ORDER BY
               time DESC LIMIT 5")) {

            while($row = $result->fetch_object()) {
                $time = date('m-d',strtotime($row->time));

                if($row->shortTitle < $row->title) {
                    $shortTitle = $row->shortTitle.'...';
                }
                else {
                    $shortTitle = $row->shortTitle;
                }

                $this->simList[] = array(
                    "time" => $time,
                    "id" => $row->id,
                    "shortTitle" => $shortTitle,
                    "title" => $row->title
                );
            }
            return $this->simList;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_summary() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT summary FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->summary = $row->summary;
            return $this->summary;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_gallery_thumbs() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT gallery FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            if(!empty($row->gallery)) {
                $this->gallery = $row->gallery;
                $urls = explode(', ', $this->gallery);
                foreach($urls as $key => $value) {
                    $images[$key] = '<li><img width="50px" src="'.DATA_PATH.'/images/posts/'.$value.'" title="'.$value.'" alt="'.$value.'" /></li>';
                }
                $this->gallery = implode(' | ', $images);
                return $this->gallery;
                $result->close();
            }
            else {
                return "";
            }
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_galleryList() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT gallery FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->galleryList = $row->gallery;
            return $this->galleryList;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_body() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT body FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->body = $row->body;
            return $this->body;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_links() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT links FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            if(!empty($row->links)) {
                $this->string = $row->links;
                $urls = explode(', ', $this->string);
                foreach($urls as $key => $value) {
                    $links[$key] = '<li><a href="http://'.$value.'" rel="external" title="'.$value.'">'.$value.'</a></li>';
                }
                $this->string = implode('<li> | </li>', $links);
                return $this->string;
            }
            else {
                return "";
            }
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_linksList() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT links FROM ".$prefix."posts WHERE id = ".$this->id." ORDER BY id DESC")) {
            $row = $result->fetch_object();
            $this->linkList = $row->links;
            return $this->linkList;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_next() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT id, title FROM ".$prefix."posts WHERE id > ".$this->id." AND draft='0' ORDER BY id LIMIT 0,1")) {
            while($nextRow = $result->fetch_object()) {
                $this->next = '<li class="next"><a href="page/2/news/article/'.$nextRow->id.'/'.htmlentities(strtolower(str_replace( " ", "-", $nextRow->title))).URL_EXTENSION.'">'.$nextRow->title.'</a></li>';
            }
            return $this->next;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function get_prev() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT id, title FROM ".$prefix."posts WHERE id < ".$this->id." AND draft='0' ORDER BY id DESC LIMIT 0,1")) {
            while($prevRow = $result->fetch_object()) {
                $this->prev = '<li class="prev"><a href="page/2/news/article/'.$prevRow->id.'/'.htmlentities(strtolower(str_replace( " ", "-", $prevRow->title))).URL_EXTENSION.'">'.$prevRow->title.'</a></li>';
            }
            return $this->prev;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function checked() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT headline FROM ".$prefix."posts WHERE id = '".$this->id."'")) {
            $row = $result->fetch_object();
            if($row->headline == "1") {
                $this->checked = "checked";
            }
            return $this->checked;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function checkbox_draft() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT draft FROM ".$prefix."posts WHERE id = '".$this->id."'")) {
            $row = $result->fetch_object();
            if($row->draft == "1") {
                $this->draft = "checked";
            }
            return $this->draft;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

    function checkbox_featured() {
        $connect = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        if($result = $connect->query("SELECT featured FROM ".$prefix."posts WHERE id = '".$this->id."'")) {
            $row = $result->fetch_object();
            if($row->featured == "1") {
                $this->featured = "checked";
            }
            return $this->featured;
            $result->close();
        } else {
            return $connect->error;
        }
        $connect->close();
    }

}
?>
